<template>
    <div class="app-container">
        <div class="customContainer ">
            <!-- 搜索头 -->
            <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
                <el-form-item>
                    <el-checkbox true-label="1" false-label="0" v-model="formInline.look_del" @change="lookDelChange"
                        border>
                        查看已删门店
                    </el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-date-picker v-model="pickerDate" value-format="yyyy.MM.dd" type="daterange" align="right"
                        unlink-panels range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :picker-options="pickerOptions"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-select filterable placeholder="选择门店" v-model="formInline.store_id" style="width:300px">
                        <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="formInline.card_no" placeholder="请输入手机号" @keyup.enter.native="searchClick">
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <dj-button size="small" @search="searchClick" />
                </el-form-item>
            </el-form> 
            <!-- 表格 -->
            <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small"
                @sort-change='sortChange'>
                <el-table-column align="center" label='序号' width="50">
                    <template slot-scope="scope">
                        <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="mobile" label="手机号"></el-table-column>
                <el-table-column align="center" prop="card_name" label="会员卡类型"></el-table-column>
                <el-table-column align="center" prop="tickets" label="总存票数" sortable></el-table-column>
                <el-table-column align="center" prop="coins" label="消费币数"></el-table-column>
            </el-table>
        </div>
        <!-- 自定义底部 -->
        <div class="bottom_box">
            <el-pagination class="pull-left" background @current-change="handleCurrentChange"
                :current-page.sync="currentPage" :page-size="20" layout="prev, pager, next, jumper"
                :total="totalDataNumber">
            </el-pagination>
        </div>
    </div>
</template>
<script>
    import {
        getList,
        getPage,
        getStoreList,
    } from "@/api/storage_control/deposit_ticket_all.js";
    import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils';
    export default {
        data() {
            return {
                list: null,
                listLoading: false,
                storeList: null,
                currentPage: 1,
                totalDataNumber: 0,
                formInline: {
                    store_id: '',
                    begin_time: '',
                    end_time: '',
                    card_no: '',
                    look_del: '0'
                },
                pickerOptions: pickerOptionsF(),
                pickerDate: pickerDateF()
            }
        },
        beforeRouteEnter(to, from, next) {
            next((vm) => {
                if (!vm.storeList) {
                    vm.getStore();
                }
            })
        },
        methods: {
            lookDelChange() {
                this.getStore();
            },
            sortChange(column, prop, order) {
                this.formInline.sort = column.prop;
                this.formInline.sort_by = column.order == "descending" ? "desc" : "asc";
                this.handleCurrentChange(this.currentPage)
            },
            getStore() {
                getStoreList({ look_del: this.formInline.look_del, category_id: 344 }).then(response => {
                    this.storeList = response;
                   let data = response.filter(res => 
                        res.id == this.$store.getters.optionId
                    )
                    if(data.length>0){
                        this.formInline.store_id = data[0].id;
                    } else {
                        if(this.storeList.length > 0) {
                            this.formInline.store_id = this.storeList[0].id;
                        }
                    }
                    if (this.list == null) {
                        this.searchClick()
                    }
                });
            },
            // 搜索
            searchClick() {
                optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
                    this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
                    this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                    this.fetchPage();
                });
            },
            // 获取分页
            fetchPage() {
                this.listLoading = true;
                getPage(this.formInline).then(response => {
                    this.totalDataNumber = response.page * 20;
                    this.fetchData();
                })
            },
            // 分页
            handleCurrentChange(val) {
                this.fetchData(val);
            },
            fetchData(val = '') {//es6默认值
                this.listLoading = true;
                getList(this.formInline, val).then(response => {
                    response.forEach(val => {
                        val.tickets *= 1;
                        switch (val.type_id) {
                            case '0':
                                val.card_name = val.card_type_id == 2 ? '临时卡' : val.card_type_id == 0 ? '普通会员卡' : '精英会员卡'
                                break;
                            case '1':
                                val.card_name = val.card_type_id == 10 ? '10次卡' : '20次卡'
                                break;
                            case '2':
                                val.card_name = '季卡'
                                break;
                            case '3':
                                val.card_name = '年卡'
                                break;
                            case '4':
                                val.card_name = '星星卡'
                                break;
                            default:
                                val.card_name = '未知'
                                break;
                        }
                    });
                    this.list = response;
                    this.listLoading = false;
                }).catch(res=>{
                    this.listLoading=false;
                });
            }
        }
    }
</script>